<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/9/26
 * Time: 17:31
 */

namespace app\home\controller;

use app\common\logic\outExcel;
use think\Controller;

class Download extends Controller
{
    /** 导出Excel
     * 表头下标对应数据下标
     * $head Array 表头
     * $data Array 数据
     * $fileName string 导出的文件名
     * /home/download/out_need_xls
     **/
    public function out_need_xls(){
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $url = '/home/download/out_need_xls?';
            $res_url = $this->join_url($data,$url);
            return json(['code' => 0, 'msg' => 'ok', 'url' => $res_url]);
        } else {
            $data = $this->request->param();
            $fileName = '工时统计' . date('Y-m-d') . '.xls';
            $list = outExcel::exportExcel($data);
            header("Content-Type: e: applicationnd.ms-exc-excel; charset=utf8");
            header("Content-Disposition: attachment; filename={$fileName}");
            header("Content-type:text/html;charset=utf-8");
            $this->assign('data',$list);
            return view('out_need');
        }
    }

    /** 导出Excel
     * 表头下标对应数据下标
     * $head Array 表头
     * $data Array 数据
     * $fileName string 导出的文件名
     * /home/download/out_bug_xls
     **/
    public function out_bug_xls(){
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $url = '/home/download/out_bug_xls?';
            $res_url = $this->join_url($data,$url);
            return json(['code' => 0, 'msg' => 'ok', 'url' => $res_url]);
        } else {
            $data = $this->request->param();
            $fileName = 'bug统计' . date('Y-m-d') . '.xls';
            $list = outExcel::exportBugExcel($data);
            header("Content-Type: e: applicationnd.ms-exc-excel; charset=utf8");
            header("Content-Disposition: attachment; filename={$fileName}");
            header("Content-type:text/html;charset=utf-8");
            $this->assign('data', $list);
            return view('out_bug');
        }
    }

    /** 导出Excel
     * 表头下标对应数据下标
     * $head Array 表头
     * $data Array 数据
     * $fileName string 导出的文件名
     * /home/download/out_project_xls
     **/
    public function out_project_xls(){
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $url = '/home/download/out_project_xls?';
            $res_url = $this->join_url($data,$url);
            return json(['code' => 0, 'msg' => 'ok', 'url' => $res_url]);
        } else {
            $data = $this->request->param();
            $fileName = 'project统计' . date('Y-m-d') . '.xls';
            $list = outExcel::exportProjectExcel($data);
            header("Content-Type: e: applicationnd.ms-exc-excel; charset=utf8");
            header("Content-Disposition: attachment; filename={$fileName}");
            header("Content-type:text/html;charset=utf-8");
            $this->assign('data', $list);
            return view('out_project');
        }
    }

    protected function join_url($data,$url){
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $k => $v) {
                    $url .= "{$key}[]={$v}&";
                }
            } else {
                $url .= "{$key}={$value}&";
            }

        }
        return substr($url,0,-1);
    }
}